﻿using Domain.Entities;
using Domain.Services;
using Domain.Misc;
using System;

namespace UserInterface.Misc
{
    public class Session
    {
        private int userId;
        private DateTime loginDateTime;
        private DateTime lastActiveDateTime;
        private string locationUrl;
        private string currentTemplate;

        public Session(int userId, DateTime loginDateTime)
        {
            this.userId = userId;
            this.loginDateTime = loginDateTime;
            this.lastActiveDateTime = loginDateTime;

            if (UserService.GetById(userId).Theme != null)
            {
                this.currentTemplate = UserService.GetById(userId).Theme.Directory;
            }
            else
            {
                this.currentTemplate = BoardSettingsManager.Instance.Settings.DefaultTheme.Directory;
            }
        }

        public int UserId
        {
            get { return userId; }
            set { userId = value; }
        }

        public DateTime LoginDateTime
        {
            get { return loginDateTime; }
        }

        public DateTime LastActiveDateTime
        {
            get { return lastActiveDateTime; }
            set { lastActiveDateTime = value; }
        }

        public string LocationUrl
        {
            get { return locationUrl; }
            set { locationUrl = value; }
        }

        public DateTime ActiveTime
        {
            get
            {
                TimeSpan time = lastActiveDateTime.Subtract(loginDateTime);
                return new DateTime(time.Ticks);
            }
        }

        public string CurrentTemplate
        {
            get { return currentTemplate; }
            set { currentTemplate = value; }
        }

        public bool IsGuest
        {
            get { return userId == UITools.GuestUserId; }
        }

        public User User
        {
            get { return UserService.GetById(userId); }
        }
    }
}
